import Taro from '@tarojs/taro';

const uploadUrl = 'https://api.aunt.geekbuluo.com/upload/file';

const chooseImage = (): Promise<Taro.chooseImage.SuccessCallbackResult> => {
  return Taro.chooseImage({
    count: 1,
    sizeType: ['compressed'],
    sourceType: ['camera', 'album'],
  });
};

const upload = (filePath) => {
  Taro.showLoading({
    title: '上传中',
    mask: true,
  });
  return Taro.uploadFile({
    url: uploadUrl,
    filePath,
    name: 'file',
  }).then((res) => {
    Taro.hideLoading();
    const { statusCode, data } = res as never;
    if (statusCode === 200 || statusCode === 201) {
      const imgRes = JSON.parse(data);
      return imgRes.data;
    } else {
      Taro.showToast({
        title: '上传失败',
      });
    }
  });
};

export { upload, chooseImage };
